<template>
  <div class="login ">
    <div style="position: absolute; top: 30px; right: 150px; width: 50px; height: 50px; ">
      <LangSelect style="font-size: 35px; color: #2c3e50" class="right-menu-item hover-effect"></LangSelect>
    </div>

    <div class="login-form">
<!--    <h3 class="title">{{ $t('login.title') }}</h3>-->
      <div style="text-align: center; height: 70px"><img class="title" src="@/assets/images/ora_admin.png" style="width: 320px; height: 70px;"/></div>
      <el-tabs v-model="activeName" @tab-click="handleClick" >

        <el-tab-pane  :label= "$t('login.passwordToLogin')" name="first">
          <el-form ref="loginForm" :model="loginForm" :rules="loginRules" >

          <el-form-item prop="username">
            <el-input v-model="loginForm.username" type="text" auto-complete="off" :placeholder="$t('login.username')">
              <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" />
            </el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              v-model="loginForm.password"
              type="password"
              auto-complete="off"
              :placeholder="$t('login.password')"
              @keyup.enter.native="handleLogin"
            >
              <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
            </el-input>
          </el-form-item>
          <!--      <el-form-item prop="code">-->
          <!--        <el-input-->
          <!--          v-model="loginForm.code"-->
          <!--          auto-complete="off"-->
          <!--          :placeholder="$t('login.verificationCode')"-->
          <!--          style="width: 63%"-->
          <!--          @keyup.enter.native="handleLogin"-->
          <!--        >-->
          <!--          <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />-->
          <!--        </el-input>-->
          <!--        <div class="login-code">-->
          <!--          <img :src="codeUrl" @click="getCode" class="login-code-img"/>-->
          <!--        </div>-->
          <!--      </el-form-item>-->
          <el-checkbox v-model="loginForm.rememberMe" style="margin:0px 0px 25px 0px;">{{$t('login.remPassword')}}</el-checkbox>
            <el-form-item style="width:100%;">
              <el-button
                :loading="loading"
                size="medium"
                type="primary"
                style="width:100%;"
                @click.native.prevent="handleLogin"
              >
                <span v-if="!loading">{{$t('login.logIn')}}</span>
                <span v-else>{{$t('login.logging')}}...</span>
              </el-button>
            </el-form-item>
        </el-form>
        </el-tab-pane>
<!--        <el-tab-pane :label= "$t('login.emailToLogin')" name="second">-->
<!--          <el-form ref="loginForm2" :model="loginForm" :rules="loginRules2" >-->
<!--          <el-form-item prop="email">-->
<!--            <el-input v-model="loginForm.email" type="text" auto-complete="off" :placeholder="$t('login.email')">-->
<!--              <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" />-->
<!--            </el-input>-->
<!--          </el-form-item>-->
<!--          <el-form-item prop="emailCode">-->
<!--            <el-col :span="12">-->
<!--              <el-input-->
<!--                v-model="loginForm.emailCode"-->
<!--                type="password"-->
<!--                auto-complete="off"-->
<!--                :placeholder="$t('login.emailCode')"-->
<!--                @keyup.enter.native="handleLogin"-->
<!--              >-->
<!--                <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />-->
<!--              </el-input>-->
<!--            </el-col>-->
<!--            <el-col :span="11" :offset="1">-->
<!--              <el-button-->
<!--                :loading="codeLoading"-->
<!--                size="medium"-->
<!--                type="primary"-->
<!--                style="width:100%;"-->
<!--                @click.native.prevent="getEmailCode()"-->
<!--              >-->
<!--                <span >{{$t('login.getCode')}}</span>-->
<!--              </el-button>-->
<!--            </el-col>-->
<!--          </el-form-item>-->

<!--            <el-form-item style="width:100%;margin-top: 66px">-->
<!--              <el-button-->
<!--                :loading="loading"-->
<!--                size="medium"-->
<!--                type="primary"-->
<!--                style="width:100%;"-->
<!--                @click.native.prevent="handleLogin"-->
<!--              >-->
<!--                <span v-if="!loading">{{$t('login.logIn')}}</span>-->
<!--                <span v-else>{{$t('login.logging')}}...</span>-->
<!--              </el-button>-->
<!--            </el-form-item>-->
<!--          </el-form>-->
<!--        </el-tab-pane>-->
      </el-tabs>



    <!--  底部  -->
    <div class="el-login-footer">
      <span>Copyright © 2022-2023 EcoIP All Rights Reserved.</span>
    </div>
    </div>
  </div>
</template>

<script>
import { getCodeImg,getEmailCode } from "@/api/login";
import Cookies from "js-cookie";
import { encrypt, decrypt } from '@/utils/jsencrypt'
import LangSelect from '@/components/LangSelect'

import { getCookie } from '@/utils/auth'

export default {
  name: "Login",
  components:{
    LangSelect
  },
  data() {
    return {
      activeName: 'first',
      codeUrl: "",
      cookiePassword: "",
      loginForm: {
        username: "",
        password: "",
        email:null,
        emailCode:"",
        rememberMe: false,
        code: "",
        uuid: "",
        loginType:1
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", message: this.$t('login.userNotNull') }
        ],
        password: [
          { required: true, trigger: "blur", message: this.$t('login.passNotNull') }
        ],
        // code: [{ required: true, trigger: "change", message: "验证码不能为空" }]
      },
      loginRules2: {
        email : [
          { required: true, trigger: "blur", message: this.$t('login.emailNotNull') }
        ],
        // emailCode: [
        //   { required: true, trigger: "blur", message: "密码不能为空" }
        // ]
      },
      loading: false,
      codeLoading: false,
      redirect: undefined
    };
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true
    }
  },
  beforeCreate() {
    if (!getCookie('lang_setting')){
      if((navigator.language).toLowerCase().includes('ja')) {
        this.$i18n.locale = 'ja'
        this.$store.dispatch('app/setLanguage', 'ja')
      }else if((navigator.language).toLowerCase().includes('en')) {
        this.$i18n.locale = 'en'
        this.$store.dispatch('app/setLanguage', 'en')
      }else{
        this.$i18n.locale = 'zh'
        this.$store.dispatch('app/setLanguage', 'zh')
      }
    }

  },
  created() {
    // this.getCode();
    this.getCookie();
  },
  methods: {
    handleClick(tab, event) {
      console.log(tab, event);
      if(tab.name=='first'){
        this.loginForm.loginType=1
      }else{
        this.loginForm.loginType=2
      }
    },
    getCode() {
      getCodeImg().then(res => {
        this.codeUrl = "data:image/gif;base64," + res.img;
        this.loginForm.uuid = res.uuid;
      });
    },
    getEmailCode(){
      this.$refs.loginForm2.validate(valid => {
        if (valid) {
          this.codeLoading = true;
          getEmailCode({receiveAccount:this.loginForm.email}).then(res => {
            this.loginForm.uuid = res.uuid;
            this.msgSuccess(this.$t('login.sendSuccessfully'));
            this.codeLoading = false;
          }).catch(() => {
            this.codeLoading = false;
            this.msgError(this.$t('login.sendFailed'));
          });
        }
      })
    },
    getCookie() {
      const username = Cookies.get("username");
      const password = Cookies.get("password");
      const rememberMe = Cookies.get('rememberMe')
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password: password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe),
        loginType: this.loginForm.loginType
      };
    },
    handleLogin() {
      if(this.activeName=='first'){
        this.$refs.loginForm.validate(valid => {
          if (valid) {
            console.log(1,this.loginForm)
            this.loading = true;
            if (this.loginForm.rememberMe) {
              Cookies.set("username", this.loginForm.username, { expires: 30 });
              Cookies.set("password", encrypt(this.loginForm.password), { expires: 30 });
              Cookies.set('rememberMe', this.loginForm.rememberMe, { expires: 30 });
            } else {
              Cookies.remove("username");
              Cookies.remove("password");
              Cookies.remove('rememberMe');
            }
            this.$store.dispatch("Login", this.loginForm).then(() => {
              this.$router.push({ path: this.redirect || "/" }).catch(()=>{});
            }).catch(() => {
              this.loading = false;
              // this.getCode();
            });
          }
        });
      }else {
        this.$refs.loginForm2.validate(valid => {
          if (valid) {
            this.loading = true;
            this.loginForm.username=this.loginForm.email
            this.loginForm.code=this.loginForm.emailCode
            console.log(this.loginForm)
            this.$store.dispatch("Login", this.loginForm).then(() => {
              this.$router.push({ path: this.redirect || "/" }).catch(()=>{});
            }).catch(() => {
              this.loading = false;
              // this.getCode();
            });
          }
        });
      }

    }
  }
};
</script>

<style rel="stylesheet/scss" lang="scss">
.login {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  background-image: url("../assets/images/fengche.jpg");
  background-size: cover;
}
.title {
  margin: 0px auto 30px auto;
  text-align: center;
  color: #707070;
}

.login-form {
  border-radius: 6px;
  background: #ffffff;
  width: 450px;
  padding: 25px 25px 5px 25px;
  .el-input {
    height: 38px;
    input {
      height: 38px;
    }
  }
  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
  }
}
.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}
.login-code {
  width: 33%;
  height: 38px;
  float: right;
  img {
    cursor: pointer;
    vertical-align: middle;
  }
}
.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}
.login-code-img {
  height: 38px;
}
</style>
